// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router';
import store from './store';

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';





import i18n from './lang/index';

import {getToken} from './utils/auth'

Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
});

import Fragment from 'vue-fragment';

Vue.use(Fragment.Plugin);

Vue.config.productionTip = false

const whiteList = ['/login'];
router.beforeEach((to, from, next) => {
  if (getToken()) {
    console.log("token:"+getToken());
    if (to.path === '/login') {
      next({path: '/'});
    } else {
      if (store.getters.menus === undefined) {
        store.dispatch('findInfo').then(() => {
          router.addRoutes([{
            path: '/',
            component: () => import('@/views/main/Main'),
            redirect: '/home',
            hidden: true,
            children: store.getters.routes
          }]);
          i18n.mergeLocaleMessage('zh', {menu: store.getters.localeZh});
          i18n.mergeLocaleMessage('en', {menu: store.getters.localeEn});
          i18n.locale = store.getters.userInfo.local;
          next({...to});
        }).catch(() => {

        });
      } else {
        next();
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next('/login');
    }
  }
});

new Vue({
  el: '#app',
  i18n,
  router,
  store,
  components: {App},
  template: '<App/>'
})
